using System;
using System.IO;
using Toolbox.DnsService.Interfaces;

namespace Toolbox.DnsService.Services
{
    /// <summary>
    /// 控制台DNS日志记录器
    /// 作者：huobing
    /// </summary>
    public class ConsoleDnsLogger : IDnsLogger
    {
        private readonly TextWriter _output;
        private readonly TextWriter _errorOutput;
        private bool _disposed = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="output">标准输出流</param>
        /// <param name="errorOutput">错误输出流</param>
        public ConsoleDnsLogger(TextWriter? output = null, TextWriter? errorOutput = null)
        {
            _output = output ?? Console.Out;
            _errorOutput = errorOutput ?? Console.Error;
        }

        /// <summary>
        /// 记录状态信息
        /// </summary>
        /// <param name="message">消息内容</param>
        public void LogStatus(string message)
        {
            if (string.IsNullOrWhiteSpace(message))
                return;

            var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            var logMessage = $"[{timestamp}] [INFO] {message}";
            
            try
            {
                _output.WriteLine(logMessage);
                _output.Flush();
            }
            catch (Exception)
            {
                // 忽略输出异常
            }
        }

        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="exception">异常对象</param>
        public void LogError(string message, Exception? exception = null)
        {
            if (string.IsNullOrWhiteSpace(message))
                return;

            var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            var logMessage = $"[{timestamp}] [ERROR] {message}";
            
            if (exception != null)
            {
                logMessage += $"\nException: {exception.GetType().Name}: {exception.Message}";
                if (exception.StackTrace != null)
                {
                    logMessage += $"\nStackTrace: {exception.StackTrace}";
                }
            }
            
            try
            {
                _errorOutput.WriteLine(logMessage);
                _errorOutput.Flush();
            }
            catch (Exception)
            {
                // 忽略输出异常
            }
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message">调试消息</param>
        public void LogDebug(string message)
        {
            if (string.IsNullOrWhiteSpace(message))
                return;

            var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            var logMessage = $"[{timestamp}] [DEBUG] {message}";
            
            try
            {
                _output.WriteLine(logMessage);
                _output.Flush();
            }
            catch (Exception)
            {
                // 忽略输出异常
            }
        }

        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="message">警告消息</param>
        public void LogWarning(string message)
        {
            if (string.IsNullOrWhiteSpace(message))
                return;

            var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            var logMessage = $"[{timestamp}] [WARN] {message}";
            
            try
            {
                _output.WriteLine(logMessage);
                _output.Flush();
            }
            catch (Exception)
            {
                // 忽略输出异常
            }
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing">是否释放托管资源</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    try
                    {
                        _output?.Flush();
                        _errorOutput?.Flush();
                    }
                    catch
                    {
                        // 忽略刷新异常
                    }
                }
                
                _disposed = true;
            }
        }
    }
}